// pages/food/index.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    active: 1,
    show: false,
    list: [],
    list2: [],
    username: "",
    tx: "",
    value: "",
    id: "",
    ind: "",
    userlist: [],
  },

  // 隐藏评论
  hidden(e) {
    this.setData({
      show: false,
    });
  },
  // 显示评论
  showmsg(e) {
    console.log(this.data.list);
    let a = e.currentTarget.dataset.index;

    this.setData({
      show: true,
      ind: a,
      id: this.data.list[e.currentTarget.dataset.index].id,
    });
  },
  //发送评论
  send() {
    let comlist = [];
    let comment = {
      name: this.data.username,
      tx: this.data.tx,
      comment: this.data.value,
    };
    let index = this.data.ind;

    if (this.data.active == 1) {
      console.log(99999999);
      let { list } = this.data;
      comlist.push(...list[index].commentmsg, comment);
      let comments = "list[" + index + "].comment";
      let commentmsg = "list[" + index + "].commentmsg";
      wx.request({
        url: "http://localhost:8888/comment",
        data: {
          id: this.data.id,
          comment: JSON.stringify(comlist),
        },
        method: "POST",
        success: (res) => {
          console.log(res);
          this.setData({
            value: "",
            [comments]: this.data.list[index].comment + 1,
            [commentmsg]: comlist,
          });
          console.log(this.data.list);
        },
      });
    } else {
      console.log(66666666);
      let { list2 } = this.data;
      comlist.push(...list2[index].commentmsg, comment);
      console.log(comlist, this.data.id);
      let comments = "list2[" + index + "].comment";
      let commentmsg = "list2[" + index + "].commentmsg";
      wx.request({
        url: "http://localhost:8888/comment",
        data: {
          id: this.data.id,
          comment: JSON.stringify(comlist),
        },
        method: "POST",
        success: (res) => {
          console.log(res);
          this.setData({
            value: "",
            [comments]: this.data.list2[index].comment + 1,
            [commentmsg]: comlist,
          });
          console.log(this.data.list);
        },
      });
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  getInfo() {
    let that = this;
    wx.getStorage({
      key: "user",
      success(res) {
        let a = JSON.parse(res.data);
      
        that.setData({
          username: a.name,
          tx: a.tx,
        });
      },
    });
  },
  // 获取发布者信息

  onLoad: function (options) {
    // let that =this
    this.getInfo();
    if (this.data.active == 1) {
      this.getFn("http://localhost:8888/getlist");
    }
  },
  // 推荐
  getFn(url) {
    let userlist = [];
    wx.request({
      url: url, //仅为示例，并非真实的接口地址
      header: {
        "content-type": "application/json", // 默认值
      },
      method: "GET",
      success: (res) => {
        this.setData({
          list: [],
        });
        res.data.data.map((item, index) => {
          wx.request({
            url: "http://localhost:8888/getuser",
            data: {
              name: JSON.parse(item.userid).name,
            },
            method: "POST",
            success: (res) => {
              // 关注处理
              if (JSON.parse(item.userid).name == this.data.username) {
                item.oneself = 1;
              } else {
                let a = res.data.data[0].focus;
                item.focus = a ? false : true;
                item.oneself = 2;
                item.focuslist = a ? a : [];
              }

              if (item.img) {
                item.img = JSON.parse(item.img);
              }
              // 时间处理
              let front;
              let oldtime = item.create_up;
              let now = new Date(),
                year = now.getFullYear(),
                month = now.getMonth() + 1,
                day = now.getDate(),
                shi = now.getHours(),
                fen = now.getMinutes();
              let tims =
                ` ${year}` +
                "/" +
                `${month}` +
                "/" +
                `${day}` +
                " " +
                `${shi}` +
                `${fen}`;

              let nowday = tims.split(" ")[1];
              let nowtime = tims.split(" ")[2];
              let c = oldtime.substring(oldtime.length - 9).substring(0, 2);
              let d = oldtime.substring(oldtime.length - 7).split(":")[0];
              let e = oldtime.substring(oldtime.length - 7).split(":")[1];
              let days = oldtime.substring(0, oldtime.length - 9).replace(" ",'');
              // console.log(days == nowday)
              // console.log('1'+days+'1')
              // console.log('1'+nowday+'1')
              if (days == nowday) {

                console.log(9999)
                if (c == "下午") {
                  d = (d * 1 + 12) * 60 + e * 1;
                  nowtime =
                    nowtime.substring(0, 2) * 60 + nowtime.substring(0, 2) * 1;
                  let min = nowtime - d;
                  if (min >= 60) {
                    front = Math.floor(min / 60) + "小时";
                  } else {
                    front = Math.floor(min) + "分钟";
                  }
                } else {
                }
              } else {
                console.log(66666666)
                if (
                  days.substring(days.length - 1) * 1 -
                    nowday.substring(nowday.length - 1) * 1 ==
                  -1
                ) {
                  front = "一天";
                } else {
                  front = "三天";
                }
              }

              item.create_up = front + "前";

              // 名称处理
              item.userid = JSON.parse(item.userid);

              let userlists = {
                id: item.id,
                name: item.userid.name,
              };
              userlist.push(userlists);

              this.setData({
                userlist: userlist,
              });

              // 点赞处理
              if (item.likes) {
                item.likelist = JSON.parse(item.likes);
                let g = JSON.parse(item.likes).filter((i) => {
                  if (i.name.indexOf(this.data.username) == -1) {
                    item.liked = true;
                  } else {
                    item.liked = false;
                  }
                  return item;
                });
                item.likes = JSON.parse(item.likes).length;
              } else {
                item.likes = 0;
                item.liked = true;
                item.likelist = [];
              }
              item.share = item.share ? JSON.parse(item.share).length : 0;

              // 评论处理
              if (item.comment) {
                item.commentmsg = JSON.parse(item.comment);
                item.comment = JSON.parse(item.comment).length;
              } else {
                item.comment = 0;
                item.commentmsg = [];
              }
              this.setData({
                list: [...this.data.list, item],
              });
            },
          });
        });
      },
    });
  },
  // 关注
  getfocuse(e) {
    let { list } = this.data;
    let index = e.currentTarget.dataset.index;
    let focus = "list[" + index + "].focus";
    
    // console.log(list[index].focus);
    let fans = {
      name: this.data.username,
      tx: this.data.tx,
    };
    if (list[index].focus) {
      wx.request({
        url: "http://localhost:8888/focus",
        data: {
          name: this.data.username,
          focus: list[index].userid,
        },
        method: "POST",
        success: (res) => {
          console.log(res);
          this.setData({
            [focus]: false,
          });
        },
      });
      wx.request({
        url: "http://localhost:8888/fans",

        data: {
          fans: fans,
          name: list[index].userid.name,
        },
        method: "POST",
        success: (res) => {
          console.log(res);
        },
      });
    }
  },
  // 选项卡
  changeClick(e) {
    this.setData({
      active: e.target.dataset.i,
      show: false,
    });

    if (this.data.active == 1) {
      this.getFn("http://localhost:8888/getlist");
    } else {
      this.focus();
      console.log(this.data.list2);
    }
  },
  focus() {
    let userlist = [];
    wx.request({
      url: "http://localhost:8888/getuser",
      data: {
        name: this.data.username,
      },
      method: "POST",
      success: (res) => {
        // console.log(JSON.parse(res.data.data[0].focus));

        let a = JSON.parse(res.data.data[0].focus);

        a.map((i, index) => {
          wx.request({
            url: "http://localhost:8888/focususer",
            data: {
              word: i.name,
            },
            method: "POST",
            success: (res) => {
              console.log(res);
              this.setData({
                list2: [],
              });
              res.data.data.map((item, index) => {
                if (item.img) {
                  item.img = JSON.parse(item.img);
                }
                let front;
                let oldtime = item.create_up;
                let now = new Date(),
                  year = now.getFullYear(),
                  month = now.getMonth() + 1,
                  day = now.getDate(),
                  shi = now.getHours(),
                  fen = now.getMinutes();
                let tims =
                  ` ${year}` +
                  "/" +
                  `${month}` +
                  "/" +
                  `${day}` +
                  " " +
                  `${shi}` +
                  `${fen}`;

                let nowday = tims.split(" ")[1];
                let nowtime = tims.split(" ")[2];
                let c = oldtime.substring(oldtime.length - 9).substring(0, 2);
                let d = oldtime.substring(oldtime.length - 7).split(":")[0];
                let e = oldtime.substring(oldtime.length - 7).split(":")[1];
                let days = oldtime.substring(0, oldtime.length - 9);

                if (days == nowday) {
                  if (c == "下午") {
                    d = (d * 1 + 12) * 60 + e * 1;
                    nowtime =
                      nowtime.substring(0, 2) * 60 +
                      nowtime.substring(0, 2) * 1;
                    let min = nowtime - d;
                    if (min >= 60) {
                      front = Math.floor(min / 60) + "小时";
                    } else {
                      front = Math.floor(min) + "分钟";
                    }
                  } else {
                  }
                } else {
                  if (
                    days.substring(days.length - 1) * 1 -
                      nowday.substring(nowday.length - 1) * 1 ==
                    -1
                  ) {
                    front = "一天";
                  } else {
                    front = "三天";
                  }
                }

                item.create_up = front + "前";

                // 名称处理
                item.userid = JSON.parse(item.userid);

                let userlists = {
                  id: item.id,
                  name: item.userid.name,
                };
                userlist.push(userlists);

                this.setData({
                  userlist: userlist,
                });

                // 点赞处理
                if (item.likes) {
                  item.likelist = JSON.parse(item.likes);
                  let g = JSON.parse(item.likes).filter((i) => {
                    if (i.name.indexOf(this.data.username) == -1) {
                      item.liked = true;
                    } else {
                      item.liked = false;
                    }
                    return item;
                  });
                  item.likes = JSON.parse(item.likes).length;
                } else {
                  item.likes = 0;
                  item.liked = true;
                  item.likelist = [];
                }
                item.share = item.share ? JSON.parse(item.share).length : 0;

                // 评论处理
                if (item.comment) {
                  item.commentmsg = JSON.parse(item.comment);
                  item.comment = JSON.parse(item.comment).length;
                } else {
                  item.comment = 0;
                  item.commentmsg = [];
                }
                this.setData({
                  list2: [...this.data.list2, item],
                });

                // console.log(this.data.list2);
              });
            },
          });
        });
      },
    });

  },
  // 分享
  onShareAppMessage(options) {
    let shareObj = {
      title: "精选优惠券",
      path: "/pages/selected", // 默认当前页的url
      imageUrl: "../images/logo.png",
      success: function (res) {
        console.log(res, "成功");
      },
      fail: function (res) {
        console.log(res, "失败");
      },
    };
    if (option.form === "button") {
      var eData = options.target.dataset;
      console.log(eData.name);
    }

    console.log(shareObj);
    return shareObj;
  },
  // 点赞
  liked(e) {
    let index = e.currentTarget.dataset.index;
    let lists = e.currentTarget.dataset.list;
    console.log(lists);
    // let { list, list2 } = this.data;
    let liked = "list[" + index + "].liked";
    let liked2 = "list2[" + index + "].liked";
    let likedlist = "list[" + index + "].likelist";
    let likes = "list[" + index + "].likes";
    let likes2 = "list2[" + index + "].likes";
    let likelist = lists[index].likelist;
    let name = {
      name: this.data.username,
    };
    if (lists[index].liked) {
      likelist.push(name);
      let a = lists[index].id;
      let b = JSON.stringify(likelist);
      this.psotlike(a, b);
      this.setData({
        [liked]: false,
        [liked2]: false,
        [likes]: lists[index].likes + 1,
        [likes2]: lists[index].likes + 1,
      });
    } else {
      likelist.indexOf(name);
      let a = lists[index].id;
      let indexs;
      likelist.filter((item, index) => {
        if (item.name.indexOf(this.data.username) != -1) {
          console.log(index);
          indexs = index;
        }
      });
      let newlist = likelist.splice(0, indexs);
      let b = newlist ? newlist : [];
      this.psotlike(a, b);
      this.setData({
        [liked]: true,
        [liked2]: true,
        [likes]: lists[index].likes - 1,
        [likes2]: lists[index].likes - 1,
        [likedlist]: b,
      });
    }
  },
  psotlike(id, list) {
    wx.request({
      url: "http://localhost:8888/likes",
      data: {
        id: id,
        like: list,
      },
      method: "POST",
      success: (res) => {
        console.log(res.data);
      },
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
});
